package ex08.pyrmont.startup;

import com.sky.JULLog.MyLogger;
import ex04.pyrmont.MyLoggerV4Adapter;
import ex08.pyrmont.core.SimpleWrapperV8;
import ex08.pyrmont.core.SimpleContextConfigV8;
import org.apache.catalina.Connector;
import org.apache.catalina.Context;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.Loader;
import org.apache.catalina.Wrapper;
import org.apache.catalina.connector.http.HttpConnector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.loader.WebappClassLoader;
import org.apache.catalina.loader.WebappLoader;
import org.apache.naming.resources.ProxyDirContext;

import java.util.logging.Logger;

/**
 * invoke: http://localhost:8080/servlet/ModernServlet or  http://localhost:8080/servlet/PrimitiveServlet
 */
public final class BootstrapV8 {

    private static Logger log = MyLogger.getLogger(BootstrapV8.class);


    public static void main(String[] args) {


        System.setProperty("catalina.base", System.getProperty("user.dir"));
        Connector connector = new HttpConnector();
        Wrapper wrapper1 = new SimpleWrapperV8();
        wrapper1.setName("Primitive");
        wrapper1.setServletClass("PrimitiveServlet");
        Wrapper wrapper2 = new SimpleWrapperV8();
        wrapper2.setName("Modern");
        wrapper2.setServletClass("ModernServlet");

        Context context = new StandardContext();
        StandardContext standardContext = (StandardContext) context;
        standardContext.setDebug(4);
        // StandardContext's start method adds a default mapper
        context.setPath("/myApp");    //这里设置 context path ,  so 请求路径需要 配置下;
        context.setDocBase("myApp");

        context.addChild(wrapper1);
        context.addChild(wrapper2);

        // context.addServletMapping(pattern, name);
        context.addServletMapping("/servlet/PrimitiveServlet", "Primitive");
        context.addServletMapping("/servlet/ModernServlet", "Modern");
        // add ContextConfig. This listener is important because it configures
        // StandardContext (sets configured to true), otherwise StandardContext  won't start
        LifecycleListener listener = new SimpleContextConfigV8();
        ((Lifecycle) context).addLifecycleListener(listener);

        Loader loader = new WebappLoader();    // here is our loader 使用了 tomcat 原生的 loader
        WebappLoader webappLoader = (WebappLoader)  loader;
        webappLoader.setDebug(4);
//        webappLoader.setReloadable(true);
        context.setLoader(loader);// associate the loader with the Context 将会触发 webApploader#propertyChange()
        context.setLogger(new MyLoggerV4Adapter(context.getClass()));  //配置了自己 的 logger ;
        context.setReloadable(true);
        connector.setContainer(context);

        try {
            connector.initialize();
            ((Lifecycle) connector).start();
            ((Lifecycle) context).start();
            // now we want to know some details about WebappLoader
            WebappClassLoader classLoader = (WebappClassLoader) loader.getClassLoader();
            log.warning("Resources' docBase: " + ((ProxyDirContext) classLoader.getResources()).getDocBase());
            String[] repositories = classLoader.findRepositories();
            for (String repository : repositories) {
                log.warning("repository: " + repository);
            }
            // make the application wait until we press a key.
            System.in.read();
            ((Lifecycle) context).stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
